/*
 * OptionsScreen.java
 *
 * Created on September 13, 2006, 3:10 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package Experience.Screens;

import Comm.Util.Exceptions.PreferencesException;
import Comm.Util.Logger;
import Comm.Util.Preferences;
import Experience.*;
import javax.microedition.lcdui.*;

/**
 *
 * @author dmgreen
 */
public class PreferencesScreen extends Form implements CommandListener {
    
    TextField myAddressTF;
    TextField myUserIDTF;
    ChoiceGroup myDebugModeCG;
    ExpUI myMIDlet;
    
    /** Creates a new instance of OptionsScreen */
    public PreferencesScreen(ExpUI mid) {
        super("MAHI Options");
        
        myMIDlet = (ExpUI) mid;
        
        this.addCommand(new Command("Save", Command.OK, 1));
        this.addCommand(new Command("Back", Command.BACK, 1));
//this.addCommand(new Command("HELP", Command.HELP, 2));
	//this.addCommand(new Command("OK", Command.OK, 1));
	
        // set itself as the command listener
        this.setCommandListener(this);
        
        populateForm();
    }
    
    private void populateForm() {
        // add an option for experience sample recipient email address
    
        myAddressTF = new TextField("Email:", "dmgreen@cc.gatech.edu", 50, TextField.EMAILADDR);
        this.append(myAddressTF);
        
        myUserIDTF = new TextField("User ID", "1", 10, TextField.NUMERIC);
        this.append(myUserIDTF);
        
        myDebugModeCG = new ChoiceGroup(
			"Debug: ",
			Choice.POPUP,
			new String[] {"Off", "On"},
			null);
        myDebugModeCG.setSelectedIndex(0,true);
        this.append(myDebugModeCG);
        
        
    }
    
    private String getAddress() {
        return myAddressTF.getString();
    }

    private String getUID() {
        return myUserIDTF.getString();
    }
    
    private boolean getDebugMode() {
        String m = myDebugModeCG.getString(myDebugModeCG.getSelectedIndex()); 
        
        if (m == "On") {
            return true;
        } else {
            return false;
        }
    }
    
    public void commandAction(Command com, Displayable dis) {
	String label = com.getLabel();
	
        if ("Save".equals(label)) {
            myMIDlet.setAddress(getAddress());
            
            //myMIDlet.setUserID(getUID());
            Integer i = Integer.valueOf( getUID() );
            try {
                Preferences.getPreferences().setUserID( i.intValue() );
                //Logger.getLog().addLogMessage("User ID successfully set to " + i.intValue(), Logger.MSG_DEBUG);
            } catch (PreferencesException pe) {
                Logger.getLog().addLogMessage("Unable to set user ID in Preferences: " + pe.getMessage(), Logger.MSG_ERROR);
            }
            
            boolean d_m = getDebugMode();
            try {
                Preferences.getPreferences().setDebugMode( d_m );
                //Logger.getLog().addLogMessage("Debug mode successfully set to " + d_m, Logger.MSG_DEBUG);
            } catch (PreferencesException pe) {
                Logger.getLog().addLogMessage("Unable to set debug mode in Preferences: " + pe.getMessage(), Logger.MSG_ERROR);
            }
            
            
        }
        else if ("Back".equals(label)) {
            myMIDlet.unshowPreferencesScreen();
        }
        else if ("Exit".equals(label)) {
            //do nothing
        }             
    }
    
    public void reconstitute() {
        //only reconstitutes for address now, but should handle multiple
        // maybe use a hashmap ?
        
        String address = ((ExpUI) myMIDlet).getAddress();
        //String uid     = ((ExpUI) myMIDlet).getUserID();
        
        Logger.getLog().addLogMessage("Getting user id ...", Logger.MSG_DEBUG);
        int uid;
        try {
            uid = Preferences.getPreferences().getUserID();
            Logger.getLog().addLogMessage("Got " + uid, Logger.MSG_DEBUG);
        } catch (PreferencesException pe) {
            Logger.getLog().addLogMessage("Error getting user id while populating screen: " + pe.getMessage(), Logger.MSG_ERROR);
            uid = 0;
        }
        
        myUserIDTF.setString(String.valueOf(uid));
        
        
        Logger.getLog().addLogMessage("Getting debug mode...", Logger.MSG_DEBUG);
        boolean debug_mode;
        
        debug_mode = Preferences.getPreferences().getDebugMode();
       
        Logger.getLog().addLogMessage("Got " + debug_mode, Logger.MSG_DEBUG);
        if (debug_mode == false) { 
            myDebugModeCG.setSelectedIndex(0,true);
        } else {
            myDebugModeCG.setSelectedIndex(1,true);
        }
        
        myAddressTF.setString(address);
        
    }
    
}
